﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERF;
using FirstPerson.Objects;
using FirstPerson;
using Microsoft.Xna.Framework;
using FirstPerson.States;
using ERF.Physics;

namespace FirstPerson.Gametypes
{
    public class GhostMatch : GameType
    {
        private int killLimit = 0;
        
        public int KillLimit
        {
            get { return killLimit; }
            set { killLimit = value; }
        }

        public GhostMatch(ERFGame game)
            : base(game)
        {
            ((FirstPerson.PlayerHandler)this.game.PlayerHandler).PlayerKill += new EventHandler<PlayerKillEventArgs>(GhostMatch_PlayerKill);
            ((FirstPerson.PlayerHandler)this.game.PlayerHandler).PlayerDie += new EventHandler<PlayerDieEventArgs>(GhostMatch_PlayerDie);
        }

        void GhostMatch_PlayerDie(object sender, PlayerDieEventArgs e)
        {
            e.Victim.Deaths++;
            if (!e.Victim.Equals(e.Killer))
                e.Killer.Kills++;

            ResetPlayer(e.Victim);
        }


        void GhostMatch_PlayerKill(object sender, PlayerKillEventArgs e)
        {
            e.Victim.Deaths++;

            if (e.Victim.Equals(e.Killer))
            {
                GameConsole.GetInstance.AddMessage(string.Format("{0} commited suicide!", e.Killer.Name));
            }
            else
            {
                GameConsole.GetInstance.AddMessage(string.Format("{0} killed {1}!", e.Killer.Name, e.Victim.Name));
                e.Killer.Kills++;
            }

            if (e.Killer is HumanPlayer)
                SpawnGhost(e.Victim.Position);
        }

        public override bool IsGameOver(GameTime gameTime)
        {
            if (gameTime.TotalRealTime - StartTime >= new TimeSpan(0, 0, DurationTime))
                return true;

            List<Player> players = ((FirstPerson.PlayerHandler)game.PlayerHandler).PlayersList;

            foreach (Player player in players)
            {
                if (killLimit == Score(player))
                    return true;
            }

            return false;
        }

        private void SpawnGhost(Vector3 position)
        {
            Map map = PlayingState.GetInstance(game.StateMachine).Map;

            AIPlayer aiPlayer = new AIPlayer(this.game, map.WayPoints, new Random());
            aiPlayer.PhysicsBody.position = position; //new Vector3(100, 300, 100);
            aiPlayer.CurrentWeapon.Visible = false;

            PlayingState.GetInstance(this.game.StateMachine).PhysicsEngine.AddBody(aiPlayer.PhysicsBody);
            this.game.CurrentSceneGraph.Items.Add(aiPlayer);
        }

        public override int Score(FirstPerson.Objects.Player player)
        {
            return player.Kills - player.Deaths;
        }

        public override Vector3 PickSpawnpointPosition()
        {
            Vector2 pos = this.PickRandomSpawnPoint().Position;

            float height = PlayingState.GetInstance(this.game.StateMachine).GameWorld.GetHeightFromXZ(pos.X, pos.Y);

            return new Vector3(pos.X, height, pos.Y);
        }
    }
}
